package com.ido.otaclient.realtk;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;

import com.ido.otaclient.R;
import com.ido.otaclient.base.BaseActivity;
import com.ido.otaclient.base.IDLog;
import com.ido.otaclient.dialog.WaitingDialog;
import com.ido.otaclient.dialog.WhiteConfirmDialog;
import com.ido.otaclient.module.Veneer;
import com.ido.otaclient.util.ResourceUtil;
import com.ido.otaclient.util.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Copyright (c) 2019 深圳市爱都科技有限公司. All rights reserved.
 *
 * @Author: xyb
 * @CreateDate: 2020-02-25 10:07
 * @description
 */
public class RealtkActivity extends BaseActivity implements RealtkContract.View{
    private static final String TAG = "RealtkActivity";


    private static final String EXTRA_VENEER_LIST = "veneer_list";
    /**
     * 返回按钮
     */
    @BindView(R.id.title_leftBtn)
    ImageButton mTitleLeftBtn;
    /**
     * 标题
     */
    @BindView(R.id.title_text)
    TextView mTitleText;
    /**
     * 选择文件
     */
    @BindView(R.id.iv_choose)
    ImageButton mIvChoose;
    /**
     * 文件名称
     */
    @BindView(R.id.tv_choose_file)
    TextView mTvChooseFile;
    /**
     * 升级文件版本
     */
    @BindView(R.id.et_version)
    EditText mEtVersion;
    /**
     * 设备id
     */
    @BindView(R.id.et_device_id)
    EditText mEtDeviceId;
    /**
     * 确认配置
     */
    @BindView(R.id.btn_confirm)
    Button mBtnConfirm;

    /**
     * 设备集合
     */
    private List<Veneer> mVeneerList = new ArrayList<>();
    /**
     * 文件路径
     */
    private String mFilePath;

    private RealtkContract.Presenter mPresenter;

    /**
     * 选择文件的回调
     */
    private static final int REQUEST_CODE_FILE = 1000;

    public static void startActivity(Activity activity, List<Veneer> veneerList) {
        Intent intent = new Intent(activity, RealtkActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable(EXTRA_VENEER_LIST, (Serializable) veneerList);
        intent.putExtras(bundle);
        activity.startActivity(intent);
    }

    @Override
    protected void onCreateTask(Bundle savedInstanceState) {
        super.onCreateTask(savedInstanceState);
        setContentView(R.layout.activity_realtk);

        initView();
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        if (intent != null) {
            Bundle bundle = intent.getExtras();
            mVeneerList = (List<Veneer>) bundle.getSerializable(EXTRA_VENEER_LIST);

            for (Veneer veneer : mVeneerList) {
                IDLog.d(TAG, "initData: " + veneer.getIp());
            }
        }

        mPresenter = new RealtkPresenter(this);
        mPresenter.initConfig();
        showMessage(ResourceUtil.getString(R.string.upload_confirm_config));
    }

    private void initView() {
        mTitleText.setText("Realtk升级");

    }

    @OnClick(R.id.title_leftBtn)
    public void back(View view) {
        ActivityCompat.finishAfterTransition(this);
    }

    @OnClick(R.id.btn_confirm)
    public void toConfirm(View view) {
        mPresenter.confirmConfig();
    }

    @OnClick(R.id.iv_choose)
    public void chooseFile(View view){
        mPresenter.chooseFile(REQUEST_CODE_FILE,RealtkActivity.this);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        mPresenter.onActivityResult(RealtkActivity.this,requestCode,resultCode,data);
    }

    @Override
    public void showLoading() {
        WaitingDialog.showDialog(RealtkActivity.this);
    }

    @Override
    public void hideLoading() {
        WaitingDialog.hideDialog();
    }

    @Override
    public void showMessage(String message) {
        ToastUtils.show(message);
    }

    @Override
    public void setChooseDrawable() {
        mIvChoose.setImageDrawable(ResourceUtil.getDrawable(R.drawable.ic_collection));
    }

    @Override
    public void showTvChooseFile(boolean isVisible) {
        mTvChooseFile.setVisibility(isVisible ? View.VISIBLE : View.GONE);
    }

    @Override
    public void setTvChooseFileName(String chooseFileName) {
        mTvChooseFile.setText(chooseFileName);
    }

    @Override
    public String getTvChooseFileName() {
        return mTvChooseFile.getText().toString();
    }

    @Override
    public String getChooseFileVersion() {
        return mEtVersion.getText().toString();
    }

    @Override
    public void setFilePath(String path) {
        mFilePath = path;
    }

    @Override
    public void setFileVersion(String version) {
        mEtVersion.setText(version);
    }

    @Override
    public String getFilePath() {
        return mFilePath;
    }

    @Override
    public void back() {
        ActivityCompat.finishAfterTransition(this);
    }

    @Override
    public void showConfirmCancel() {
        WhiteConfirmDialog mWhiteConfirmDialog = WhiteConfirmDialog.newInstance(
                ResourceUtil.getString(R.string.upload_file_fail),ResourceUtil.getString(R.string.confirm),
                ResourceUtil.getString(R.string.cancel),true);
        mWhiteConfirmDialog.setOnConfirmListener(view -> {
            //重新下发固件
            mPresenter.uploadAndConfirmFile();
            //uploadAndConfirmFile();
            mWhiteConfirmDialog.dismissAllowingStateLoss();
        });
        mWhiteConfirmDialog.setCancelListener(view -> {
            //取消弹框
            mWhiteConfirmDialog.dismissAllowingStateLoss();
            hideLoading();
        });

        mWhiteConfirmDialog.setCancelable(false);
        mWhiteConfirmDialog.show(getSupportFragmentManager());
    }

    @Override
    public void setDeviceId(String deviceId) {
        mEtDeviceId.setText(deviceId);
    }

    @Override
    public String getDeviceId() {
        return mEtDeviceId.getText().toString();
    }

    @Override
    public List<Veneer> getVeneerList() {
        return mVeneerList;
    }

    @Override
    public void setVeneerList(List<Veneer> veneerList) {
        if(mVeneerList != null)
            mVeneerList.clear();
        mVeneerList.addAll(veneerList);
    }

    @Override
    public void setPresenter(@NonNull RealtkContract.Presenter presenter) {
        mPresenter = presenter;
    }
}
